package controls.questions {
	
	import data.Result;
	
	import events.ExperimentEvent;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.HSlider;
	import mx.controls.Label;
	import mx.controls.RadioButtonGroup;
	import mx.events.FlexEvent;
	import mx.events.ItemClickEvent;
	
	/**
	 * @author Brent Kersanske
	 */ 
	public class MultipleChoiceQuestionClass extends VBox {
		
		private var _questionID:String;
		private var _questionHeaderText:String;
		private var _minReadTime:Number;
		private var _scenarioText:String;
		private var _questionText:String;
		private var _correctOption:String;
		private var _option1Text:String;
		private var _option2Text:String;
		private var _option3Text:String;
		private var _option4Text:String;
		private var _option5Text:String;
		
		private var timer:Timer;
		private var responseTime:Number;
		
		[Bindable]
		public var readyForQuestionButton:Button;
		
		[Bindable]
		public var countDownLabel:Label;
		
		[Bindable]
		public var questionContentBox:VBox;
		
		[Bindable]
		public var answersRadioButtonGroup:RadioButtonGroup;
		
		[Bindable]
		public var submitAnswerButton:Button;
		
		[Bindable]
		public var difficultyRatingBox:HBox;
		
		[Bindable]
		public var difficultySlider:HSlider;
		
		[Bindable]
		public var questionCompleteButton:Button;
		
		public function MultipleChoiceQuestionClass() {
			addEventListener(FlexEvent.CREATION_COMPLETE, handleCreationComplete);	
		}
		
		private function handleCreationComplete(e:FlexEvent):void {
			startMinimumReadingTimer();
		}
		
		[Bindable]
		public function set questionID(questionID:String):void {
			_questionID = questionID;
		}
		
		public function get questionID():String {
			return _questionID;
		}
		
		[Bindable]
		public function set questionHeaderText(questionHeaderText:String):void {
			_questionHeaderText = questionHeaderText;
		}
		
		public function get questionHeaderText():String {
			return _questionHeaderText;
		}
		
		[Bindable]
		public function set scenarioText(scenarioText:String):void {
			_scenarioText = scenarioText;
		}
		
		public function get scenarioText():String {
			return _scenarioText;
		}
		
		public function set minReadTime(minTime:Number):void {
			_minReadTime = minTime;
		}
		
		public function get minReadTime():Number {
			return _minReadTime;
		}
		
		[Bindable]
		public function set questionText(questionText:String):void {
			_questionText = questionText;
		}
		
		public function get questionText():String {
			return _questionText;
		}
		
		public function set correctOption(correctOption:String):void {
			_correctOption = correctOption;
		}
		
		public function get correctOption():String {
			return _correctOption;
		}
		
		[Bindable]
		public function set option1Text(option1Text:String):void {
			_option1Text = option1Text;
		}
		
		public function get option1Text():String {
			return _option1Text;
		}
		
		[Bindable]
		public function set option2Text(option2Text:String):void {
			_option2Text = option2Text;
		}
		
		public function get option2Text():String {
			return _option2Text;
		}
		
		[Bindable]
		public function set option3Text(option3Text:String):void {
			_option3Text = option3Text;
		}
		
		public function get option3Text():String {
			return _option3Text;
		}
		
		[Bindable]
		public function set option4Text(option4Text:String):void {
			_option4Text = option4Text;
		}
		
		public function get option4Text():String {
			return _option4Text;
		}
		
		[Bindable]
		public function set option5Text(option5Text:String):void {
			_option5Text = option5Text;
		}
		
		public function get option5Text():String {
			return _option5Text;
		}
		
		public function clearAll():void {
			_option1Text = "";
			_option2Text = "";
			
		}
		
		public function startMinimumReadingTimer():void {
			this.countDownLabel.text = String(this.minReadTime);
			var minReadTime:Timer = new Timer(1000, this.minReadTime);
			minReadTime.addEventListener(TimerEvent.TIMER, handleMinReadTimerTick);
			minReadTime.addEventListener(TimerEvent.TIMER_COMPLETE, handleMinReadTimerComplete);
			minReadTime.start();
		}
		
		private function handleMinReadTimerTick(e:TimerEvent):void {
			this.countDownLabel.text = String(Number(this.countDownLabel.text) - 1);
		}
		
		private function handleMinReadTimerComplete(e:TimerEvent):void {
			this.countDownLabel.text 			= "";
			this.readyForQuestionButton.enabled = true;
		}
		
		/**
		 * 
		 */ 
		public function handleReadyForQuestion(e:Event):void {
			this.readyForQuestionButton.enabled = false;
			this.questionContentBox.visible		= true;
			this.timer							= new Timer(10);
			this.timer.start();
		}
		
		public function enableSubmissionButton(e:ItemClickEvent):void {
			this.submitAnswerButton.enabled = true;
		}
		
		public function handleAnswerSubmission(e:Event):void {
			this.timer.stop();
			this.responseTime 					 = this.timer.currentCount / 100;
			this.answersRadioButtonGroup.enabled = false;
			this.submitAnswerButton.enabled		 = false;
			this.difficultyRatingBox.visible 	 = true;
			this.questionCompleteButton.visible  = true;
		}
		
		public function handleQuestionComplete(e:Event):void {
			var theResult:Result = new Result(this.questionID, this.correctOption, getOptionNumber(this.answersRadioButtonGroup.selectedValue.toString()), this.responseTime, this.difficultySlider.value);
			this.dispatchEvent(new ExperimentEvent(ExperimentEvent.NEW_RESULT, true, false, theResult));
		}
		
		private function getOptionNumber(option:String):String {
			if(option == this.option1Text) {
				return "1";
			} else if(option == this.option2Text) {
				return "2";
			} else if(option == this.option3Text) {
				return "3";
			} else if(option == this.option4Text) {
				return "4";
			} else if(option == this.option5Text) {
				return "5";
			} else {
				return option;
			}
		}
	}
}